package j2;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class jsub {
    public static String s1() {
        int a, b;
        String a1, b1, s1;
        String c;
        Random random = new Random();
        while (true) {
            a = random.nextInt(100);
            b = random.nextInt(100);
            a1 = String.valueOf(a);
            b1 = String.valueOf(b);
            s1 = String.valueOf(a + b);
            if (Integer.parseInt(s1) <= 100) {
                c = String.format("%3s + %3s = %3s", a1, b1, s1);
                return c;
            } else {
                s1();
            }
        }
    }
    public static String s2() {
        int a, b;
        String a1,b1,s2;
        String c;
        Random random = new Random();
        while (true) {
            a = random.nextInt(100);
            b = random.nextInt(100);
            a1 = String.valueOf(a);
            b1 = String.valueOf(b);
            s2 = String.valueOf(a - b);
            if (Integer.parseInt(s2) >= 0) {
                c = String.format("%3s - %3s = %3s", a1, b1, s2);
                return c;
            } else {
                s2();
            }
        }
    }
    public static String gT() {
        Date T = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = formatter.format(T);
        return d;
    }

    public static String ss(String[]g,int num1,int num2,int expected){
        String q2;
        if(num1+num2==expected&&num1!=0) {
            q2 = String.format("%3s + %3s = %3s", g[0], g[1], g[2]);
        }
        else if(num1-num2==expected){
            q2 = String.format("%3s - %3s = %3s", g[0], g[1], g[2]);
        }
        else {
            int l=-expected;
            q2 = String.format("%3s - %3s = %3s", g[0], g[1], l);
        }
        return q2;
}
    public static void sE(String[] A, int j) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("src/main/java/j2/errortext.txt", true))) {
            writer.write(jsub.gT());
            writer.newLine();
            for (int i = 0; i < j; i++) {
                if (A[i] != null) {
                    writer.write(A[i]);
                    writer.newLine();
                }
            }
            writer.write("----------");
            writer.newLine();
        } catch (IOException e) {
            System.out.println("保存错误答案时出错: " + e.getMessage());
        }
    }
    public static void g3(String[] e,int i){
        System.out.println("已进入题库:");
        System.out.println();
        Pattern pattern = Pattern.compile("^\\s*\\d{1,3}\\s*[-+]\\s*\\d{1,3}\\s*=\\s*-?\\d{1,3}\\s*$");
        Scanner sc = new Scanner(System.in);
        System.out.println("增（1）/删（2）/退出");
        String q= sc.nextLine();
        if(q.equals("1")){
            System.out.println("请输入算式(两数小于100大于0)");
            String q1=sc.nextLine();
            if(pattern.matcher(q1).matches()) {
                String[] g = new String[3];
                Pattern p = Pattern.compile("\\d+");
                Matcher m = p.matcher(q1);
                for (int j = 0; j < 3; j++) {
                    if (m.find()) {
                        g[j] = m.group();
                    }
                }
                int num1 = Integer.parseInt(g[0]);
                int num2 = Integer.parseInt(g[1]);
                int expected = Integer.parseInt(g[2]);
                if ((num1 + num2 != expected)&& (Math.abs(num1 - num2) != expected)) {
                    System.out.println("添加算式不符合答案错误!");
                    g3(e, i);
                } else {
                    String q2=ss(g,num1,num2,expected);
                    System.out.println("添加成功");
                    try (BufferedWriter writer = new BufferedWriter(new FileWriter("src/main/java/j2/data.txt", true))) {
                        writer.write(q2);
                        writer.newLine();
                        System.out.println("保存成功!");
                        i++;
                        e[i] = q2;
                    } catch (IOException ex) {
                        System.out.println("保存错误答案时出错: " + ex.getMessage());
                    }
                }
            }
             else{
                System.out.println("添加算式不符合格式！");
                g3(e,i);
            }
        } else if(q.equals("2")) {
            System.out.println("输入删除的序号:");
            String l=sc.nextLine();
            int li=Integer.parseInt(l);
            if(li<=i){
                try {
                    String filePath = "src/main/java/j2/data.txt";
                    BufferedReader reader = new BufferedReader(new FileReader(filePath));
                    StringBuilder contentBuilder = new StringBuilder();
                    String currentLine;
                    int currentLineNumber = 0;
                    while ((currentLine = reader.readLine()) != null) {
                        currentLineNumber++;
                        if (currentLineNumber != li) {
                            contentBuilder.append(currentLine).append(System.lineSeparator());
                        }
                    }
                    reader.close();
                    try (FileWriter writer = new FileWriter(filePath)) {
                        writer.write(contentBuilder.toString());
                    }
                    System.out.println("已删除序号为 " + li + " 的题");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            else {
                System.out.println("无效输入！");
                g3(e, i);
            }
        }else{
            System.out.println("已退出管理题库");
            jrun.a();
        }
    }
}
